{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "import pandas as pd\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "pro = ts.pro_api('0ba8feef618e5db7b1ebb65538fe51e4aef69fb3cbf709d44128f313')\n"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "交易所代码 CFFEX-中金所 DCE-大商所 CZCE-郑商所 SHFE-上期所 INE-上海国际能源交易中心"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>pre_settle</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>settle</th>\n",
       "      <th>vol</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AP.ZCE</td>\n",
       "      <td>20220310</td>\n",
       "      <td>10122.0</td>\n",
       "      <td>10250.0</td>\n",
       "      <td>9992.0</td>\n",
       "      <td>10138.0</td>\n",
       "      <td>9820.0</td>\n",
       "      <td>10019.0</td>\n",
       "      <td>10022.0</td>\n",
       "      <td>120472.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AP2203.ZCE</td>\n",
       "      <td>20220310</td>\n",
       "      <td>9000.0</td>\n",
       "      <td>9000.0</td>\n",
       "      <td>9050.0</td>\n",
       "      <td>9050.0</td>\n",
       "      <td>9050.0</td>\n",
       "      <td>9050.0</td>\n",
       "      <td>9050.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AP2204.ZCE</td>\n",
       "      <td>20220310</td>\n",
       "      <td>9828.0</td>\n",
       "      <td>9819.0</td>\n",
       "      <td>9692.0</td>\n",
       "      <td>9791.0</td>\n",
       "      <td>9602.0</td>\n",
       "      <td>9678.0</td>\n",
       "      <td>9659.0</td>\n",
       "      <td>89.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>AP2205.ZCE</td>\n",
       "      <td>20220310</td>\n",
       "      <td>10122.0</td>\n",
       "      <td>10250.0</td>\n",
       "      <td>9992.0</td>\n",
       "      <td>10138.0</td>\n",
       "      <td>9820.0</td>\n",
       "      <td>10019.0</td>\n",
       "      <td>10022.0</td>\n",
       "      <td>120472.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>AP2210.ZCE</td>\n",
       "      <td>20220310</td>\n",
       "      <td>8200.0</td>\n",
       "      <td>8231.0</td>\n",
       "      <td>8134.0</td>\n",
       "      <td>8207.0</td>\n",
       "      <td>7950.0</td>\n",
       "      <td>7998.0</td>\n",
       "      <td>8095.0</td>\n",
       "      <td>28158.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>239</th>\n",
       "      <td>ZC2212.ZCE</td>\n",
       "      <td>20220310</td>\n",
       "      <td>NaN</td>\n",
       "      <td>828.4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>828.4</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>240</th>\n",
       "      <td>ZC2301.ZCE</td>\n",
       "      <td>20220310</td>\n",
       "      <td>840.2</td>\n",
       "      <td>840.2</td>\n",
       "      <td>809.8</td>\n",
       "      <td>851.4</td>\n",
       "      <td>756.0</td>\n",
       "      <td>756.0</td>\n",
       "      <td>815.4</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>241</th>\n",
       "      <td>ZC2302.ZCE</td>\n",
       "      <td>20220310</td>\n",
       "      <td>NaN</td>\n",
       "      <td>798.4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>798.4</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>242</th>\n",
       "      <td>ZC2303.ZCE</td>\n",
       "      <td>20220310</td>\n",
       "      <td>NaN</td>\n",
       "      <td>803.4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>803.4</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>243</th>\n",
       "      <td>ZCL.ZCE</td>\n",
       "      <td>20220310</td>\n",
       "      <td>NaN</td>\n",
       "      <td>849.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>849.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>244 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ts_code trade_date  pre_close  pre_settle    open     high     low  \\\n",
       "0        AP.ZCE   20220310    10122.0     10250.0  9992.0  10138.0  9820.0   \n",
       "1    AP2203.ZCE   20220310     9000.0      9000.0  9050.0   9050.0  9050.0   \n",
       "2    AP2204.ZCE   20220310     9828.0      9819.0  9692.0   9791.0  9602.0   \n",
       "3    AP2205.ZCE   20220310    10122.0     10250.0  9992.0  10138.0  9820.0   \n",
       "4    AP2210.ZCE   20220310     8200.0      8231.0  8134.0   8207.0  7950.0   \n",
       "..          ...        ...        ...         ...     ...      ...     ...   \n",
       "239  ZC2212.ZCE   20220310        NaN       828.4     NaN      NaN     NaN   \n",
       "240  ZC2301.ZCE   20220310      840.2       840.2   809.8    851.4   756.0   \n",
       "241  ZC2302.ZCE   20220310        NaN       798.4     NaN      NaN     NaN   \n",
       "242  ZC2303.ZCE   20220310        NaN       803.4     NaN      NaN     NaN   \n",
       "243     ZCL.ZCE   20220310        NaN       849.0     NaN      NaN     NaN   \n",
       "\n",
       "       close   settle       vol  \n",
       "0    10019.0  10022.0  120472.0  \n",
       "1     9050.0   9050.0       1.0  \n",
       "2     9678.0   9659.0      89.0  \n",
       "3    10019.0  10022.0  120472.0  \n",
       "4     7998.0   8095.0   28158.0  \n",
       "..       ...      ...       ...  \n",
       "239      NaN    828.4       0.0  \n",
       "240    756.0    815.4      20.0  \n",
       "241      NaN    798.4       0.0  \n",
       "242      NaN    803.4       0.0  \n",
       "243      NaN    849.0       0.0  \n",
       "\n",
       "[244 rows x 10 columns]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_futures = pro.fut_daily(trade_date='20220310', exchange='CZCE', \n",
    "                           fields='ts_code,trade_date,pre_close,pre_settle,open,high,low,close,settle,vol')\n",
    "df_futures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_df(tscode, startdate = '20180101', enddate='20220323'):\n",
    "    df = pro.fut_daily(ts_code=tscode, start_date=startdate, end_date= enddate)\n",
    "    \n",
    "    if df is None:\n",
    "        print('none')\n",
    "        return\n",
    "    df['trade_date'] = df['trade_date'].apply(lambda x: datetime.strptime(x,'%Y%m%d'))\n",
    "    df = df.set_index('trade_date')\n",
    "    df = df.sort_index()\n",
    "    df['dayofweek'] = df.index.dayofweek\n",
    "    return df\n",
    "\n",
    "#df_futures = pro.fut_daily(trade_date='20181113', exchange='CZCE', fields='ts_code,trade_date,pre_close,pre_settle,open,high,low,close,settle,vol')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>pre_settle</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>settle</th>\n",
       "      <th>change1</th>\n",
       "      <th>change2</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "      <th>oi</th>\n",
       "      <th>oi_chg</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>238</th>\n",
       "      <td>SA2201.ZCE</td>\n",
       "      <td>20210122</td>\n",
       "      <td>1772.0</td>\n",
       "      <td>1779.0</td>\n",
       "      <td>1775.0</td>\n",
       "      <td>1793.0</td>\n",
       "      <td>1764.0</td>\n",
       "      <td>1772.0</td>\n",
       "      <td>1778.0</td>\n",
       "      <td>-7.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>350.0</td>\n",
       "      <td>1244.70</td>\n",
       "      <td>1558.0</td>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>239</th>\n",
       "      <td>SA2201.ZCE</td>\n",
       "      <td>20210121</td>\n",
       "      <td>1780.0</td>\n",
       "      <td>1764.0</td>\n",
       "      <td>1780.0</td>\n",
       "      <td>1792.0</td>\n",
       "      <td>1767.0</td>\n",
       "      <td>1772.0</td>\n",
       "      <td>1779.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>457.0</td>\n",
       "      <td>1625.67</td>\n",
       "      <td>1533.0</td>\n",
       "      <td>226.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>240</th>\n",
       "      <td>SA2201.ZCE</td>\n",
       "      <td>20210120</td>\n",
       "      <td>1735.0</td>\n",
       "      <td>1734.0</td>\n",
       "      <td>1745.0</td>\n",
       "      <td>1785.0</td>\n",
       "      <td>1737.0</td>\n",
       "      <td>1780.0</td>\n",
       "      <td>1764.0</td>\n",
       "      <td>46.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>489.0</td>\n",
       "      <td>1725.18</td>\n",
       "      <td>1307.0</td>\n",
       "      <td>313.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>241</th>\n",
       "      <td>SA2201.ZCE</td>\n",
       "      <td>20210119</td>\n",
       "      <td>1730.0</td>\n",
       "      <td>1713.0</td>\n",
       "      <td>1720.0</td>\n",
       "      <td>1748.0</td>\n",
       "      <td>1720.0</td>\n",
       "      <td>1735.0</td>\n",
       "      <td>1734.0</td>\n",
       "      <td>22.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>231.0</td>\n",
       "      <td>800.97</td>\n",
       "      <td>994.0</td>\n",
       "      <td>91.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>242</th>\n",
       "      <td>SA2201.ZCE</td>\n",
       "      <td>20210118</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1714.0</td>\n",
       "      <td>1695.0</td>\n",
       "      <td>1730.0</td>\n",
       "      <td>1679.0</td>\n",
       "      <td>1730.0</td>\n",
       "      <td>1713.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>989.0</td>\n",
       "      <td>3388.88</td>\n",
       "      <td>903.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        ts_code trade_date  pre_close  pre_settle    open    high     low  \\\n",
       "238  SA2201.ZCE   20210122     1772.0      1779.0  1775.0  1793.0  1764.0   \n",
       "239  SA2201.ZCE   20210121     1780.0      1764.0  1780.0  1792.0  1767.0   \n",
       "240  SA2201.ZCE   20210120     1735.0      1734.0  1745.0  1785.0  1737.0   \n",
       "241  SA2201.ZCE   20210119     1730.0      1713.0  1720.0  1748.0  1720.0   \n",
       "242  SA2201.ZCE   20210118        NaN      1714.0  1695.0  1730.0  1679.0   \n",
       "\n",
       "      close  settle  change1  change2    vol   amount      oi  oi_chg  \n",
       "238  1772.0  1778.0     -7.0     -1.0  350.0  1244.70  1558.0    25.0  \n",
       "239  1772.0  1779.0      8.0     15.0  457.0  1625.67  1533.0   226.0  \n",
       "240  1780.0  1764.0     46.0     30.0  489.0  1725.18  1307.0   313.0  \n",
       "241  1735.0  1734.0     22.0     21.0  231.0   800.97   994.0    91.0  \n",
       "242  1730.0  1713.0     16.0     -1.0  989.0  3388.88   903.0     NaN  "
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "tscode = \"sa2201.ZCE\"\n",
    "df = pro.fut_daily(ts_code=tscode, start_date='20180101', end_date='20220323')\n",
    "df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'none' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-84-a9c4b823edd9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mget_df\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtscode\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-83-391ad14d2da1>\u001b[0m in \u001b[0;36mget_df\u001b[1;34m(tscode, startdate, enddate)\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpro\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfut_daily\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mts_code\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtscode\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstart_date\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mstartdate\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend_date\u001b[0m\u001b[1;33m=\u001b[0m \u001b[0menddate\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m     \u001b[1;32mif\u001b[0m \u001b[0mdf\u001b[0m \u001b[1;32mis\u001b[0m \u001b[0mnone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'none'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m         \u001b[1;32mreturn\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'none' is not defined"
     ]
    }
   ],
   "source": [
    "df = get_df(tscode)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Index' object has no attribute 'dayofweek'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-75-b67f35c6b42b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mtscode\u001b[0m \u001b[1;33m=\u001b[0m\u001b[1;34m'RB2201.SHF'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mtscode\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"sa201.ZCE\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mget_df\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtscode\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;31m#获取主力合约TF.CFX每日对应的月合约\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-5-1c64c056e9df>\u001b[0m in \u001b[0;36mget_df\u001b[1;34m(tscode)\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_index\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'trade_date'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort_index\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m     \u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'dayofweek'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdayofweek\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'Index' object has no attribute 'dayofweek'"
     ]
    }
   ],
   "source": [
    "tscode = 'rb2205.SHF'\n",
    "tscode ='RB2201.SHF'\n",
    "tscode = \"sa201.ZCE\"\n",
    "df = get_df(tscode)\n",
    "\n",
    "#获取主力合约TF.CFX每日对应的月合约\n",
    "#df_rb = pro.fut_mapping(ts_code='rb.SHF')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>mapping_ts_code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211220</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211217</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211216</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211215</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211214</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211213</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211210</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211209</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211208</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211207</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211206</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211203</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211202</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211201</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211130</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211129</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211126</td>\n",
       "      <td>RB2205.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211125</td>\n",
       "      <td>RB2201.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>72</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211124</td>\n",
       "      <td>RB2201.SHF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73</th>\n",
       "      <td>RB.SHF</td>\n",
       "      <td>20211123</td>\n",
       "      <td>RB2201.SHF</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ts_code trade_date mapping_ts_code\n",
       "54  RB.SHF   20211220      RB2205.SHF\n",
       "55  RB.SHF   20211217      RB2205.SHF\n",
       "56  RB.SHF   20211216      RB2205.SHF\n",
       "57  RB.SHF   20211215      RB2205.SHF\n",
       "58  RB.SHF   20211214      RB2205.SHF\n",
       "59  RB.SHF   20211213      RB2205.SHF\n",
       "60  RB.SHF   20211210      RB2205.SHF\n",
       "61  RB.SHF   20211209      RB2205.SHF\n",
       "62  RB.SHF   20211208      RB2205.SHF\n",
       "63  RB.SHF   20211207      RB2205.SHF\n",
       "64  RB.SHF   20211206      RB2205.SHF\n",
       "65  RB.SHF   20211203      RB2205.SHF\n",
       "66  RB.SHF   20211202      RB2205.SHF\n",
       "67  RB.SHF   20211201      RB2205.SHF\n",
       "68  RB.SHF   20211130      RB2205.SHF\n",
       "69  RB.SHF   20211129      RB2205.SHF\n",
       "70  RB.SHF   20211126      RB2205.SHF\n",
       "71  RB.SHF   20211125      RB2201.SHF\n",
       "72  RB.SHF   20211124      RB2201.SHF\n",
       "73  RB.SHF   20211123      RB2201.SHF"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_rb[df_rb['trade_date'] < '20211221'].head(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_shfe = pro.fut_daily(trade_date='20220310', exchange='SHFE', fields='ts_code,trade_date,pre_close,pre_settle,open,high,low,close,settle,vol')\n",
    "\n",
    "columns = ['ts_code', 'close','change1','dayofweek']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ts_code      22\n",
       "close        22\n",
       "change1      22\n",
       "dayofweek    22\n",
       "dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "#df0=df[df['dayofweek'] == 0][columns]\n",
    "dfbig = df0[df0['change1'] < 0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ts_code       243\n",
       "pre_close     242\n",
       "pre_settle    243\n",
       "open          243\n",
       "high          243\n",
       "low           243\n",
       "close         243\n",
       "settle        243\n",
       "change1       243\n",
       "change2       243\n",
       "vol           243\n",
       "amount        243\n",
       "oi            243\n",
       "oi_chg        241\n",
       "dayofweek     243\n",
       "dtype: int64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "def desc_week(df):\n",
    "    columns = ['ts_code', 'close','change1','dayofweek']\n",
    "    colname = 'change1'\n",
    "    df = df[columns]\n",
    "    for week in range(0,5):\n",
    "        df_week = df[df['dayofweek'] == week]\n",
    "        df_big = df_week[df_week['change1'] > 0]\n",
    "        df_small = df_week[df_week['change1'] < 0]\n",
    "        print(df_week[colname].count(), df_big[colname].count(), df_big[colname].mean(),df_small[colname].count() ,df_small[colname].mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "\"['dayofweek'] not in index\"",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-78-fd7a95d74ca3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdesc_week\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-45-a0735e6120ec>\u001b[0m in \u001b[0;36mdesc_week\u001b[1;34m(df)\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[0mcolumns\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'ts_code'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'close'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'change1'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'dayofweek'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[0mcolname\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'change1'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m     \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mweek\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m         \u001b[0mdf_week\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'dayofweek'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mweek\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\vnstudio\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   2804\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mis_iterator\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2805\u001b[0m                 \u001b[0mkey\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2806\u001b[1;33m             \u001b[0mindexer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_listlike_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mraise_missing\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   2807\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2808\u001b[0m         \u001b[1;31m# take() does not accept boolean indexers\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\vnstudio\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_get_listlike_indexer\u001b[1;34m(self, key, axis, raise_missing)\u001b[0m\n\u001b[0;32m   1551\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1552\u001b[0m         self._validate_read_indexer(\n\u001b[1;32m-> 1553\u001b[1;33m             \u001b[0mkeyarr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindexer\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mo\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_axis_number\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mraise_missing\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mraise_missing\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1554\u001b[0m         )\n\u001b[0;32m   1555\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mkeyarr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindexer\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\vnstudio\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_validate_read_indexer\u001b[1;34m(self, key, indexer, axis, raise_missing)\u001b[0m\n\u001b[0;32m   1644\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"loc\"\u001b[0m \u001b[1;32mand\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mraise_missing\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1645\u001b[0m                 \u001b[0mnot_found\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0max\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1646\u001b[1;33m                 \u001b[1;32mraise\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mf\"{not_found} not in index\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1647\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1648\u001b[0m             \u001b[1;31m# we skip the warning on Categorical/Interval\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: \"['dayofweek'] not in index\""
     ]
    }
   ],
   "source": [
    "desc_week(df)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
